package org.dairy.farms.nandana.web.controller;

import org.dairy.farms.model.*;
import org.dairy.farms.nandana.core.NandanaUtils;
import org.dairy.farms.nandana.core.logic.CheckupManagementLogic;
import org.dairy.farms.nandana.web.view.CheckupDetailsView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * User: gduggirala
 * Date: Aug 18, 2011
 * Time: 7:02:36 PM
 */
@Controller
public class CheckupController {
    private static final Logger logger = LoggerFactory.getLogger(CheckupController.class);
    @Autowired
    CheckupManagementLogic checkupManagementLogic;
    @Autowired
    private NandanaUtils nandanaUtils;

    @RequestMapping(value = "/checkup/checkupDetails", method = RequestMethod.GET)
    public void getCheckupDetails(@RequestParam(value = "id", required = true) Long livestockId, Model model,
                                  @RequestParam(value = "page", required = true) int page,
                                  @RequestParam(value = "start", required = true) int start,
                                  @RequestParam(value = "limit", required = true) int limit) {
        List<Checkup> checkupList = checkupManagementLogic.getCheckupDetails(livestockId, page, start, limit);
        model.addAttribute("checkupList", checkupList);
        model.addAttribute("totalCount", checkupList.size());
    }

    @RequestMapping(value = "checkup/getAllCheckupTypes", method = RequestMethod.GET)
    public void getAllCheckupTypes(Model model) {
        List<CheckupType> checkTypesList = checkupManagementLogic.getAllCheckupTypes();
        model.addAttribute("checkTypesList", checkTypesList);
        model.addAttribute("totalCount", checkTypesList.size());
    }

    @RequestMapping(value = "checkup/getAllDiseaseTypes.json", method = RequestMethod.GET)
    public void getAllDiseases(Model model) {
        List<MedicalAttentionType> medicalAttentionTypesList = checkupManagementLogic.getAllDiseases();
        model.addAttribute("medicalAttentionTypesList", medicalAttentionTypesList);
        model.addAttribute("totalCount", medicalAttentionTypesList.size());
    }

    @RequestMapping(value = "checkup/getAllVaccineTypes", method = RequestMethod.GET)
    public void getAllVaccineTypes(@RequestParam(value = "id", required = true) Long checkupTypeId,Model model) {
        List<VaccineType> vaccineTypeList = checkupManagementLogic.getAllVaccineTypes();
        model.addAttribute("vaccineTypeList", vaccineTypeList);
        model.addAttribute("totalCount", vaccineTypeList.size());
    }

    @RequestMapping(value = "checkup/updateCheckupDetails.json", method = RequestMethod.POST)
    public void updateCheckupDetails(Model model, @RequestParam(value = "checkupId", required = false) Long checkupId,
                                     @RequestParam(value = "personId", required = true) Long docId,
                                     @RequestParam(value = "expenseAmount", required = true) Float expenseAmount,
                                     @RequestParam(value = "dateOfPayment", required = true) String dateOfPayment,
                                     @RequestParam(value = "dateOfCheckup", required = true) String dateOfCheckup,
                                     @RequestParam(value = "vaccineTypes", required = false) String vaccineTypes,
                                     //@RequestParam(value = "checkupExpenseId", required = false) Long checkupExpenseId,
                                     @RequestParam(value = "diseaseTypes", required = false) String diseaseTypes,
                                     @RequestParam(value = "checkupTypeId", required = true) Long checkupTypeId,
                                     @RequestParam(value = "livestockId", required = true) Long livestockId,
                                     @RequestParam(value = "note", required = false) String note) {
        CheckupDetailsView checkupDetailsView = new CheckupDetailsView();
        checkupDetailsView.setCheckupId(checkupId);
        checkupDetailsView.setCheckupTypeId(checkupTypeId);
        //checkupDetailsView.setCheckupExpenseId(checkupExpenseId);
        checkupDetailsView.setDateOfCheckup(nandanaUtils.getDate(dateOfCheckup));
        checkupDetailsView.setDateOfPayment(nandanaUtils.getDate(dateOfPayment));
        checkupDetailsView.setDocId(docId);
        checkupDetailsView.setVaccineTypes(vaccineTypes!=null?vaccineTypes.split(","):null);
        checkupDetailsView.setMedicalAttentionTypes(diseaseTypes != null ? diseaseTypes.split(",") : null);
        checkupDetailsView.setLivestockId(livestockId);
        checkupDetailsView.setExpenseAmount(expenseAmount);
        checkupDetailsView.setNotes(note);
        Checkup checkupDetail = checkupManagementLogic.updateCheckupDetails(checkupDetailsView);
        model.addAttribute("checkup", checkupDetail);
    }
}
